
$(function() {

	$("input[id='customer']:visible").autocomplete(Test.customer);

	$("input[id='customer']:visible").blur(function() {
		var a = $(this).val();
		// Pay.supplier(a);
		
		var body = { };
		body.id = a;
		$.getJSON('contact.php?do=Customer.ajax_id', body, function(json) {
			$("#a_name").val(json.name);
			$("#a_fullname").val(json.fullname + "(" + json.code + ")");
			$("#a_address").val(json.address);
			$("#a_web").val(json.url);
			
			var s = $("#name").val();
			if (s == null || s == '') {
				$("#name").val(json.code);
			}
			
			$("#emp").val(json.emp);
		});
		
		var body = {};
		body.q = a;
		
		$.getJSON('contact.php?do=Customer.ajax_linkman', body, function(json) {			
			Common.linkman(json);
		});
	});
	
	$("#label_linkman_a").hide();
	
	/*
	_currency($('#currency').val());
	
	$('#currency').change(function() {
		var s = $(this).val();
		_currency(s);
	});*/
	
	$("input[id='date']:visible").datepicker(_date1_option);
	
	total();
	
	$("input[id='emp']:visible").autocomplete(Test.emp);
	
	$("textarea").css("width", 400);	
});

function _update_currency(currency, exchange) {
	var ret = new Array();
	$.each(data, function(k, v) {
		if (is_null(v.currency) || v.currency == '-') {
			v.currency = currency;
			v.rate = exchange;
		}
		
		ret.push(v);
	});
	
	return ret;
}

function total() {
	var total = 0;
	var amount = 0;
	var rmb = 0;
	$.each(data, function(k, v) {		
		if (v.ok) {
			total = fxadd(total, v.amount*v.price);
			// amount = fxadd(total, v.amount);
			// rmb = fxadd(total, v.rmb);
		}		
	});
	
	// alert(total);
	$("#total_t").html(total);
	$("#amount").val(total);
	// $("#amount_t").html(total);
	// $("#rmb_t").html(rmb);
}

function _currency(s) {
	if (s == null || s == '-')　{		
		return false;
	}
	
	var body = { };
	body.currency = s;
	$.post('finance.php?do=Finance.ajax_exchange', body, function(s) {
		exchange = s;
		$('#rate').val(s);
		
		_update();
	});
}

function _update() {
	var a = $("#amount").val();
	var b = $("#currency").find("option:selected").text();

	Currency.upper(a, b, "#label_amount");
	
	_rmb();
} 

function _rmb() {
	var a = $("#amount").val();
	var b = $('#rate').val();
	var rmb = fxmul(a, b);
	
	$('#rmb').val(rmb);	
}
